﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Collections.Specialized;
using Job.Core.Module;

namespace Job.Core.Data
{
    /// <summary>
    /// 数据库仓储提供程序基础类。
    /// </summary>
    public abstract class DbRepositoryProvider : IInitialize
    {
        #region Properties

        /// <summary>
        /// 开启模块缓存
        /// </summary>
        public bool IsTableCache { get; set; }

        /// <summary>
        /// 模块表前缀。
        /// </summary>
        public string TablePrefix { get; private set; }

        /// <summary>
        /// 数据库连接节名称。
        /// </summary>
        public string ConnectionStringName { get; private set; }

        /// <summary>
        /// 对该模块数据访问类的引用。
        /// </summary>
        public DbHelper DbHelper { get; private set; }

        #endregion        

        #region Overrides

        /// <summary>
        /// 初始化模块提供程序。
        /// </summary>
        /// <param name="name">模块名称。</param>
        /// <param name="config">模块配置。</param>
        public virtual void Initialize(string name, NameValueCollection config)
        {
            //连接字串。
            if (!string.IsNullOrEmpty(config["connectionStringName"]))
            {
                ConnectionStringName = config["connectionStringName"];
                DbHelper = new DbHelper(ConnectionStringName);
            }

            config.Remove("connectionStringName");

            //表前缀。
            if (!string.IsNullOrEmpty(config["tablePrefix"]))
                TablePrefix = config["tablePrefix"];

            config.Remove("tablePrefix");
        }

        #endregion

    }

}
